﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Cms.Render;
using Portal.Web.Model;
using Portal.Cms.Bussiness;
using Portal.Cms.Model;
using Portal.Core;
using Portal.Web.Utility;
using Portal.Cms.Utility;

namespace Portal.Cms.Render
{
    public class CmsPermissionRenderBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonRootUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   " <script language=javascript>\r\n" +
                   "    function ClientCheck(Id)\r\n"+
                   "    {\r\n"+
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        CheckedIds=document.getElementById('txtCheckIds').value;\r\n"+
                   "        Checked=document.getElementById('chk'+Id).checked;\r\n"+
                   "        OAjaxOut= Portal.Cms.Render.CmsPermissionRenderBll.ServerSideProcessCheck(SiteId,CheckedIds,Id,Checked).value;\r\n" +
                   "        document.getElementById('txtCheckIds').value=OAjaxOut.HtmlContent;\r\n"+
                   "    }\r\n"+
                   " </script>\r\n" +
                   " <input id=txtCheckIds type=\"hidden\">\r\n"+
                   " <div class=FormFunction id=divListForm>\r\n" +
                   "    <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   //"            <li onclick=\"javascript:ClientSave();\"><img class=image src=\""+ButtonRootUrl+"/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonRootUrl + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "    </div>\r\n" +
                   "    <div id=\"divListItems\" class=tabcontent>\r\n"+
                            ServerSideReading(SiteId).HtmlContent+
                   "    </div>\r\n"+
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupDataCls[] GroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataForPermission(SiteId);
                if (GroupData.Length==0)
                {
                    OAjaxOut.HtmlContent = "Không có dữ liệu để phân quyền";
                    return OAjaxOut;
                }

                string Html =
                   "  <h3>Phân quyền truy cập cho các phân hệ sau</h3>\r\n"+
                   "  <table>\r\n";
                for (int iIndex = 0; iIndex < GroupData.Length; iIndex++)
                {
                    Html +=  "   <tr>\r\n" +
                             "       <td class=tdIndex>"+(iIndex+1)+"</td>\r\n"+
                             "       <td class=tdGroupDataName>" + GroupData[iIndex].GroupDataName + "</td>\r\n" +
                             "       <td class=tdPermission><input type=button class=button value=\"Phân quyền\" onclick=\"javascript:window.open('Permission.aspx?ptc=" + GroupDataPermission.StaticId + "&id=" + GroupData [iIndex].GroupDataId+ "&backAspx=PermissionMan.aspx','_self')\"></td>\r\n" +
                             "   </tr>\r\n";
                }
                int Max = GroupData.Length+1;
                Html += "   <tr>\r\n" +
                            "       <td class=tdIndex>" + Max + "</td>\r\n" +
                            "       <td class=tdGroupDataName>Xem thống kê báo cáo</td>\r\n" +
                            "       <td class=tdPermission><input type=button class=button value=\"Phân quyền\" onclick=\"javascript:window.open('Permission.aspx?ptc=" + StatiticsPermission.StaticId + "&id=Statitics&backAspx=PermissionMan.aspx','_self')\"></td>\r\n" +
                            "   </tr>\r\n";
                Html += "</table>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideProcessCheck(string SiteId,string CheckedIds,string Id,bool Check)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        Id = Id.Trim();
        //        if (Check)
        //        {
        //            CheckedIds = CheckedIds +"$"+Id+ "$;";
        //        }
        //        else
        //        {
        //            CheckedIds = CheckedIds.Replace("$"+Id+"$", "");
        //        }
        //        int iFound = CheckedIds.IndexOf(";;");
        //        while (iFound != -1)
        //        {
        //            CheckedIds = CheckedIds.Replace(";;", ";");
        //            iFound = CheckedIds.IndexOf(";;");
        //        }
        //        if (CheckedIds.Length > 0)
        //        {
        //            if (CheckedIds[0].ToString().Equals(";"))
        //            {
        //                CheckedIds = CheckedIds.Substring(1);
        //            }
        //        }
        //        CheckedIds = CheckedIds.Trim();
        //        OAjaxOut.HtmlContent = CheckedIds;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}
    }
}
